//
//  MyOrderViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/4/29.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "MyOrderViewController.h"
#import "MyOrderCell.h"
#import "AppMacro.h"
#import "ParkFeeOrderDetailController.h"
#import "SVProgressHUD.h"
#import "HttpClientManager.h"
#import "DataModel.h"
#import "AppDelegate.h"
#import "BookOrderDetailViewController.h"
#import "TMCache.h"

@interface MyOrderViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation MyOrderViewController{
    
    LoginModel *loginModel;
    
    NSMutableArray *bookOrdersArray; //预约订单
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    loginModel = ApplicationDelegate.loginModel;    
}

- (void)viewWillAppear:(BOOL)animated{
    
    //默认显示预约订单
    [self initBookOrderView];
}

- (void)initBookOrderView{
    
    self.orderType = ORDERTYPE_BOOK;
    
    self.bookOrderBtn.selected = YES;
    self.bookOrderLineView.backgroundColor = MAIN_GREEN_COLOR;
    self.bookOrderLineHeightConstraint.constant = 2;
    
    //置灰 停车订单 按钮等
    self.parkFeeOrderBtn.selected = NO;
    self.parkFeeOrderLineView.backgroundColor = RGBACOLOR(193, 193, 193, 1);
    self.parkFeeOrderLineHeightConstraint.constant = 1;
    
    [self getMyBookOrders];
}

- (void)initParkFeeOrderView{
    
    self.orderType = ORDERTYPE_PARK;
    
    self.parkFeeOrderBtn.selected = YES;
    self.parkFeeOrderLineView.backgroundColor = MAIN_GREEN_COLOR;
    self.parkFeeOrderLineHeightConstraint.constant = 2;
    
    //置灰 预约订单 按钮等
    self.bookOrderBtn.selected = NO;
    self.bookOrderLineView.backgroundColor = RGBACOLOR(193, 193, 193, 1);
    self.bookOrderLineHeightConstraint.constant = 1;
}

#pragma mark - UITableView Delegate && DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return bookOrdersArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    BookParkingPortOrderModel *bookOrderModel = bookOrdersArray[row];
    
    MyOrderCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyOrderCell"];
    [cell initDataWithBookOrderModel:bookOrderModel];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    BookParkingPortOrderModel *bookOrderModel = bookOrdersArray[row];
    
    if([self.orderType isEqualToString:ORDERTYPE_BOOK]){
        
        BookOrderDetailViewController *detailController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"BookOrderDetailViewController"];
        detailController.orderNo = bookOrderModel.orderNo;
        [self.navigationController pushViewController:detailController animated:YES];
    }else{
        
        ParkFeeOrderDetailController *detailController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ParkFeeOrderDetailController"];
        [self.navigationController pushViewController:detailController animated:YES];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBActions

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)showBookOrderAction:(id)sender {
    
    //查看预约订单
    
    [self initBookOrderView];
    [self.tableView reloadData];
}

- (IBAction)showParkFeeOrderAction:(id)sender {
    
    //查看车费订单
    [self initParkFeeOrderView];
    [self.tableView reloadData];
}

#pragma mark - 查询预约订单
- (void)getMyBookOrders{
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] myBookOrdersRequestWithMobilephone:loginModel.mobilePhone WithUserId:loginModel.userId WithSucessBlock:^(JsonRetMsg *response) {
        
        [SVProgressHUD dismiss];
        
        bookOrdersArray = response.data;
        
        [[TMDiskCache sharedCache] setObject:bookOrdersArray forKey:@"Orders"];
        
        [self.tableView reloadData];
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
    }];
}

@end
